// SPDX-License-Identifier: GPL-2.0
/*
 * BUDDY GFP: GFP_DMA32
 *
 * (C) 2024.05.11 BiscuitOS <buddy.zhang@aliyun.com>
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mm.h>

static int __init BiscuitOS_init(void)
{
#ifdef CONFIG_ZONE_DMA32
	struct page *page;
	void *mem;

	/* ALLOC PHYSICAL MEMORY FROM ZONE_DMA32 */
	page = alloc_pages(GFP_DMA32, 6);
	if (!page) {
		printk("ERROR: NOT ENOUGH MEMORY\n");
		return -ENOMEM;
	}

	/* ALLOC VIRTUAL MEMORY: IGNORE */
	;

	/* BUILD PAGETABLE: LINEAR MAPPING */
	mem = page_to_virt(page);

	/* ACCESS MEMORY */
	sprintf((char *)mem, "Hello BiscuitOS! %s", page_zone(page)->name);
	printk("%s\n", (char *)mem);

	/* RECLAIM */
	__free_pages(page, 6);
#endif

	return 0;
}

static void __exit BiscuitOS_exit(void) { }

module_init(BiscuitOS_init);
module_exit(BiscuitOS_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("BiscuitOS <buddy.zhang@aliyun.com>");
MODULE_DESCRIPTION("BUDDY ALLOCATOR ON BiscuitOS");
